/**
 * File Created at 2016年6月20日
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.dam2.ledVideo;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.hw.one.dam2.service.IWeatherService;
import com.hw.one.led.ModelAndXML;
import com.hw.one.led.ScreenDataPush;

/**
 * LED数据拉取实现类
 * @author duqunxing
 *
 */
@Component
public class ScreenDataPull{
	private static Logger log = Logger.getLogger(ScreenDataPull.class);
	@Resource
	private IWeatherService weatherService;
	
	private static final String ZY_CODE = "ONE-LED";
	private static final String XA_CODE = "ONE-XAS";
	private static final String ZY_VIEW = "/tpl/one-led.xml";
	private static final String XA_VIEW = "/tpl/one-led_xi_an.xml";
	private static EnvDataPO ENV_DATA = new EnvDataPO();
//	public void pullData() {
//		try {
//			EnvDataPO envData = weatherService.getLEDData(null);
//			screenDataPush.push(envData);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	public void pullData() {
		try {
			EnvDataPO envData = (EnvDataPO) weatherService.findCityLiveWeatherByRegionCode(null);//.getLEDData(null);
			
			if(envData==null){
				envData = ENV_DATA;
				// 获取日历实例并赋予当前时间
				Calendar cNow = Calendar.getInstance();
				// 获取日历实例并赋予当前时间
				String[] weeks = new String[]{"日","一","二","三","四","五","六"};
				envData.setDayAndWeek(new SimpleDateFormat("MM月dd日").format(cNow.getTime())+" 星期" + weeks[cNow.get(Calendar.DAY_OF_WEEK)-1]);
			}else{
				ENV_DATA = envData;
			}
			ModelAndXML modelAndXML = new ModelAndXML(ZY_VIEW, envData);
			String data = modelAndXML.generate();
			log.debug("推送数据：\r\n"+data);
			if(log.isDebugEnabled()){
				parseData(envData);
			}
			ScreenDataPush dataPush = new ScreenDataPush();
			dataPush.push(ZY_CODE,data);
		} catch (Exception e) {
			log.error("推送数据错误",e);
		}
	}

	/**
	 * 渭南市 数据推送
	 * int i = 160;
			envData.setAQI(i++ +"");
			envData.setPrimaryPollutant("PM2.5");
			envData.setPollutionLevel("轻度污染");
			envData.setTodayPollutionLevel("轻度污染");
			envData.setPM10Concentration("80μg/m3");
			envData.setPM2_5Concentration("79μg/m3");
			envData.setNO2Concentration("34μg/m3");
			envData.setSO2Concentration("8μg/m3");
			envData.setCOConcentration("1.234mg/m3");
			envData.setO3Concentration("14μg/m3");
			envData.setDayAndWeek("06月15日 星期四");
			envData.setTodayTemperature("23-37°C");
			envData.setTodayWeather("多云");
			envData.setTodayWindDirection("东南风");
			envData.setTodayWindPower("5级");
			envData.setPubDate("2017-06-15 15:13:00");
			envData.setTomorrowPollutionLevel("良");
			// 明日温度
			envData.setTomorrowTemperature("23-37°C");
			// 明日污染程度
			envData.setTomorrowPollutionLevel("轻度污染");
			// 明日天气
			envData.setTomorrowWeather("多云");
			// 明日风向
			envData.setTomorrowWindDirection("东南风");
			// 明日风力
			envData.setTomorrowWindPower("5级");
	 * @author 李亚坤
	 * @createtime 2017年6月15日16:33:13
	 */
	public void pullData_weiNan() {
		try {
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("siteCode", "4100000009179009"); // 渭南市 西大明路A6C2701009
			EnvDataPO envData = weatherService.findCityLiveWeatherByRegionCodeNoSession(paramMap);//.getLEDData(null);

			if(envData==null){
				envData = ENV_DATA;
				// 获取日历实例并赋予当前时间
				Calendar cNow = Calendar.getInstance();
				// 获取日历实例并赋予当前时间
				String[] weeks = new String[]{"日","一","二","三","四","五","六"};
				envData.setDayAndWeek(new SimpleDateFormat("MM月dd日").format(cNow.getTime())+" 星期" + weeks[cNow.get(Calendar.DAY_OF_WEEK)-1]);
				return;
			}else{
				ENV_DATA = envData;
			}
			ModelAndXML modelAndXML = new ModelAndXML(XA_VIEW, envData);
			String data = modelAndXML.generate();
			log.debug("推送数据：\r\n"+data);
			if(log.isDebugEnabled()){
				parseData(envData);
			}
			ScreenDataPush dataPush = new ScreenDataPush();
			dataPush.push(XA_CODE,data);
		} catch (Exception e) {
			log.error("推送数据错误",e);
		}
	}
	
	/**
	 * 解析数据
	 * @param
	 * @return
	 */
	private void parseData(EnvDataPO envData){
	    Field[] fields = envData.getClass().getDeclaredFields();
	    log.info("推送数据解析。。。");
		for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
			String name = field.getName();
			try {
				String value = (String) field.get(envData);
				log.info("推送数据：" + name + "|" + value);
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		log.info("推送数据解析完成");
	}
}
